﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ApiTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            dispatcherTimer.Tick += DispatcherTimer_Tick;
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(1000);
            tboxApi.Text = "http://10.0.2.231/search/getUnitLink.json?xxdm=";
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            int random = new Random(Guid.NewGuid().GetHashCode()).Next(100, 1000000);
            GetWebQuest(tboxApi.Text + random);
        }

        int error = 0;
        int success = 0;
        int notSucceed = 0;
        int total = 0;

        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        private void BtnSatrt_Click(object sender, RoutedEventArgs e)
        {
            if (btnSatrt.Content.ToString() == "开始")
            {
                btnSatrt.Content = "停止";
                dispatcherTimer.Start();
            }
            else
            {
                 btnSatrt.Content = "开始";
                dispatcherTimer.Stop();
            }
        }
        private void BtnClear_Click(object sender, RoutedEventArgs e)
        {
            error = 0;
            success = 0;
            notSucceed = 0;
            total = 0;
        }
        #region HttpWebRequest实现Get请求
        /// <summary>
        /// HttpWebRequest实现Get请求
        /// </summary>
        private string GetWebQuest(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 3000;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.ContentType = "application/json";
            request.Method = "Get";
            string result = "";
            try
            {
                total++;
                tboxTotal.Text = total.ToString();
                if (tboxReceive.Text.Length > 2990)
                    tboxReceive.Text = "";
                tboxReceive.Text += url +"\n";

                using (var res = request.GetResponse() as HttpWebResponse)
                {
                    if (res.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                        result = reader.ReadToEnd();
                        success++;
                        tboxSuccess.Text = success.ToString();
                        tboxReceive.Text += result + "\n";
                    }
                    else
                    {
                        notSucceed++;
                        tboxNotSucceed.Text = notSucceed.ToString();
                        tboxReceive.Text += res.StatusCode + "\n";
                    }
                }
            }
            catch (Exception ex)
            {
                if (request != null) request.Abort();
                error++;
                tboxError.Text = error.ToString();
                tboxReceive.Text += ex.Message + "\n";

            }
            return result;
        }

        #endregion

      
    }
}
